package com.darkdev.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class Connection {
	private BufferedReader reader;
	private PrintWriter writer;
	public Connection(final Socket sock) throws Exception {
		this.reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		this.writer = new PrintWriter(new PrintWriter(sock.getOutputStream()));
		new Timer().schedule(new TimerTask(){
			public void run(){
				try {
					if (reader.ready()) {
						String cmd = reader.readLine();
						String[] args = reader.readLine().split(",");
						if (cmd.equals("info")) {
							for (String s:args) {
								writer.println(new ClassInfo(s).toString());
								writer.flush();
							}
							writer.print("\0");
							writer.flush();
						} else if (cmd.equals("info_ext")) {
							String[] cp = reader.readLine().split(" ");
							ArrayList<URL> url = new ArrayList<URL>();
							for (String c:cp) {
								try {
									url.add(new URL(c));
								} catch (Exception e) {
								}
							}
							for (String s:args) {
								writer.println(new ClassInfo(s,url.toArray(new URL[url.size()])).toString());
								writer.flush();
							}
							writer.print("\0");
							writer.flush();
						} else if (cmd.equals("exists")) {
							for (String s:args) {
								try {
									Class.forName(s,false,this.getClass().getClassLoader());
									writer.println("True");
								} catch (Exception e) {
									writer.println("False");
								}
								writer.flush();
							}
							writer.print("\0");
							writer.flush();
						} else if (cmd.equals("exists_ext")) {
							String[] cp = reader.readLine().split(" ");
							ArrayList<URL> url = new ArrayList<URL>();
							for (String c:cp) {
								try {
									url.add(new URL(c));
								} catch (Exception e) {
								}
							}
							for (String s:args) {
								try {
									Class.forName(s,false,new URLClassLoader(url.toArray(new URL[url.size()])));
									writer.println("True");
								} catch (Exception e) {
									writer.println("False");
								}
								writer.flush();
							}
							writer.print("\0");
							writer.flush();
						} else if (cmd.equals("jreinfo")) {
							writer.println("'"+System.getProperty("java.vendor","?")+"'");
							writer.println("'"+System.getProperty("java.vendor.url","?")+"'");
							writer.println("'"+System.getProperty("java.version","?")+"'");
							writer.print("\0");
							writer.flush();
						} else if (cmd.equals("close")) {
							reader.close();
							writer.close();
							sock.close();
						} else if (cmd.equals("shutdown")) {
							reader.close();
							writer.close();
							sock.close();
							System.exit(0);
						}
					}
				} catch (Exception e) {
					writer.print("\0");
					writer.flush();
				}
			}
		},1,500);
	}
}
